//
//  CJPlayPageTableVIewCell.m
//  CoJellyCartoon
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "CJPlayPageTableVIewCell.h"
#import "CJPlayPageHeader.h"
#import "CJIntroductionCollectionViewCell.h"
#import "CJCommentCollectionViewCell.h"

@interface CJPlayPageTableVIewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionViewFlowLayout *flowlayout;

@end
@implementation CJPlayPageTableVIewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self creatCollectionView];
        
    }
    return self;
}


/**
 *  collection
 */
-(void)creatCollectionView{
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.PrePlayCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero  collectionViewLayout:self.flowlayout];
    self.PrePlayCollectionView.dataSource = self;
    self.PrePlayCollectionView.delegate = self;
    self.PrePlayCollectionView.pagingEnabled = YES;
    self.PrePlayCollectionView.bounces = NO;
    [self.PrePlayCollectionView registerClass:[CJIntroductionCollectionViewCell class] forCellWithReuseIdentifier:@"CJIntroductionCollectionViewCell"];
    [self.PrePlayCollectionView registerClass:[CJCommentCollectionViewCell class] forCellWithReuseIdentifier:@"CJCommentCollectionViewCell"];
     self.flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 头视图重用池
    
//    [self.PrePlayCollectionView registerClass:[CJPlayPageHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self addSubview:self.PrePlayCollectionView];

}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.flowlayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.flowlayout.minimumInteritemSpacing = 0;
    self.flowlayout.minimumLineSpacing = 0;
    self.flowlayout.sectionHeadersPinToVisibleBounds = YES;
   
    self.PrePlayCollectionView.frame =[UIScreen mainScreen].bounds;
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.segment.selectedSegmentIndex = self.PrePlayCollectionView.contentOffset.x /[UIScreen mainScreen].bounds.size.width;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
    
    CJIntroductionCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJIntroductionCollectionViewCell" forIndexPath:indexPath];
    item.preplayPageModel = self.prePlayModel;
    
    return item;
    }
    
    CJCommentCollectionViewCell *coment = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJCommentCollectionViewCell" forIndexPath:indexPath];
    coment.commentModel = self.commnetModel;
    return coment;
}

-(void)setPrePlayModel:(CJPrePlayPageVIewModel *)prePlayModel{
    
    _prePlayModel = prePlayModel;
    
    [self.PrePlayCollectionView reloadData];
    
}
-(void)setCommnetModel:(CJCommentModel *)commnetModel{
    _commnetModel = commnetModel;
    
    [self.PrePlayCollectionView reloadData];
}
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if (kind == UICollectionElementKindSectionHeader) {
//        
//        CJPlayPageHeader *hearder = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        
//        return hearder;
//    }
//    
//    return nil;
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    
//    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
//}
@end
